#!/usr/bin/env bash

if [ -z "$WM_PROJECT" ]; then
  echo "OpenFOAM environment not found, forgot to source the OpenFOAM bashrc?"
  exit 1
fi

if [ -z "$WM_CODI_AD_MODE" ]; then
  export DF_LIB_SUFFIX=""
elif [ "$WM_CODI_AD_MODE" = "CODI_AD_FORWARD" ]; then
  export DF_LIB_SUFFIX="ADF"
elif [ "$WM_CODI_AD_MODE" = "CODI_AD_REVERSE" ]; then
  export DF_LIB_SUFFIX="ADR"
fi

case $1 in
  "opt")
    cp Make/files_Incompressible Make/files
    cp Make/options_Incompressible Make/options
    wmake -j 4 || exit 1
    ./Allclean
    cp Make/files_Compressible Make/files
    cp Make/options_Compressible Make/options
    wmake -j 4 || exit 1
    ./Allclean
    cp Make/files_Solid Make/files
    cp Make/options_Solid Make/options
    wmake -j 4 || exit 1
    ;;
  "debug")
    cp Make/files_Incompressible Make/files
    cp Make/options_Incompressible Make/options
    sed -i 's/-std=c++11/-std=c++11 -DFULLDEBUG -g -O0/g' Make/options
    wmake -j 4 || exit 1
    ./Allclean
    cp Make/files_Compressible Make/files
    cp Make/options_Compressible Make/options
    sed -i 's/-std=c++11/-std=c++11 -DFULLDEBUG -g -O0/g' Make/options
    wmake -j 4 || exit 1
    ./Allclean
    cp Make/files_Solid Make/files
    cp Make/options_Solid Make/options
    sed -i 's/-std=c++11/-std=c++11 -DFULLDEBUG -g -O0/g' Make/options
    wmake -j 4 || exit 1
    ;;
  "incompressible")
    cp Make/files_Incompressible Make/files
    cp Make/options_Incompressible Make/options
    wmake -j 4 || exit 1
    ;;
  "compressible")
    cp Make/files_Compressible Make/files
    cp Make/options_Compressible Make/options
    wmake -j 4 || exit 1
    ;;
  "solid")
    cp Make/files_Solid Make/files
    cp Make/options_Solid Make/options
    wmake -j 4 || exit 1
    ;;
  *)
    echo "Argument not valid! Options are: opt, debug, incompressible, compressible, or solid"
    echo "Example: ./Allmake opt"
    exit 1
    ;;
esac

